<?
if($_POST["selbox"] != ''){

  /**
   * NOTE: Whenever a tx is canceled make sure
   *       all the SN last Entry is _PEMBELIAN
   */

	//reauth the user, if $_SESSION["reauthed"] != Y bail out
	if($check_me_authed == "OK"){

		$today_date 			= date("d-m-Y");

		$srcDbSequence      = "MST_SEQUENCE";
    $srcDbHdr           = "PCH_HDR";
    $srcDbHdrLog        = "LOG_PCH_HDR";
    $srcDbDtl           = "PCH_DTL";
    $srcDbSubDtl        = "PCH_SUB_DTL";
    $srcDbMstModel      = "MST_MODEL";
    $srcDbInputHistory  = "MODEL_INPUT_HISTORY";
    $srcDbSupplier      = "SUPPLIER";

    $UID     = unserialize($_SESSION["serUID"]);
    $userId  = $UID->getUID();

    $COracle = new oracleDb;
    $COracle->init($_SESSION["ORCL"],
                   $_SESSION["OPASS"],
                   $_SESSION["SID"],
                   _NO_COMMENT);

    if($COracle->connect()){

			if(sizeof($_POST["selbox"]) == 0){

				$Misc->usrDie("Error: nothing to be cancel");
			}else{

				foreach($_POST["selbox"] as $cclThisJobId){

					/**
      		* Fetch global header
      		* And check the status if it is valid
      		* where STATUS = _NOT_ACTIVE
      		*
      		*/
      		$CFetchers->fetchLstDataHeader($COracle,
                                         $cclThisJobId,
                                         $srcDbHdr,
                                         $nHeader,
                                         $lstHeader);

					if($lstHeader["STATUS"][0] != _ACTIVE){

						$Misc->usrDie("Error: Tx with JobId: ".$cclThisJobId." is not yet approved.");
					}else{

						$CHandlers->procCancel($COracle,
            						           $cclThisJobId,
            						           $userId,
            						           $srcDbHdr);

						/**
            * Extra work is to update the picked
            * jobId and check if recvQty
            * Greater than 0
            * YES: set status to _OUTSTANDING
            * NO : set status to _ACTIVE
            *
            */
            $jobId    = $cclThisJobId;
            $refId    = $lstHeader["REFERENCE"][0];
            $isCancel = true;

            $periode = " TO_DATE('".$_SESSION["sysPeriode"]."','mm-YYYY') ";

            $txDate  = " TO_DATE('".date("d-m-Y",
                                        strtotime($lstHeader["TR_DATE"][0]))."',
                                 'dd-mm-YYYY') ";

            $dueDate = " TO_DATE('".date("d-m-Y",
                                        strtotime($lstHeader["DUE_DATE"][0]))."',
                                 'dd-mm-YYYY') ";

            require("controllers/procStock.php");
            require("controllers/procSnWhs.php");

            require("controllers/procAP.php");
            require("controllers/procDp.php");

            /**
            * Insert Log
            *
            */
            $CHandlers->insToDbLog($COracle,
                                   $today_date,
                                   $cclThisJobId,
                                   $userId,
                                   $_SERVER["REMOTE_ADDR"],
                                   _CANCEL_MODE,
                                   $srcDbHdrLog);
					}

				}

				//$COracle->commit();

				if($COracle->getStatus() == true){

      	  $Misc->usrMsg("Cancel Complete");
      	}else{

      	  $Misc->usrMsg("Cancel Process Interupted");
      	}

      	$COracle->dc();

				//GoBack("2","../".$_SESSION["source_caller"]);
			}
		}else{

			$Misc->usrDie("Error: Oracle connection failed");
		}
	}else{

		$Misc->usrDie("NOT AUTHED");
	}
}
?>